import time
from io import StringIO
from selenium import webdriver
from selenium.webdriver.common.by import By
import pandas as pd

# 打开目标网页
browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://data.eastmoney.com/bbsj/202212/zcfz.html')

# 获取第1～3页的网页源代码
html_list = []
max_page = 3
for page in range(1, max_page + 1):
    time.sleep(3)
    html_list.append(browser.page_source)
    if page < max_page:
        next_page = browser.find_element(By.LINK_TEXT, '下一页')
        next_page.click()
browser.quit()

# 从网页源代码中提取数据表格
data_list = []
for html in html_list:
    table_list = pd.read_html(io=StringIO(html), attrs={'style': 'display: table;'})
    data = table_list[1]
    data_list.append(data)

# 合并所有数据并导出
df = pd.concat(objs=data_list, ignore_index=True)
df.to_csv('2022年资产负债表.csv', index=False, encoding='utf-8-sig')
